import { reqGoodsInfo, reqAddOrUpdateShopCart } from "@/api";
// 封装游客什么的模块--uuid
import { getUUID } from '@/utils/uuid_token';
const state = {
    goodsInfo: {},
    // 游客的临时身份
    uuid_token: getUUID(),
};
const mutations = {
    GETGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    },
};
const actions = {
    // 获取产品信息
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId);
        if (result.code == 200) {
            console.log(result);
            commit("GETGOODSINFO", result.data)
        }
    },
    //将产品加入到购物车
    async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
        // 加入购物车返回的解构；加入购物车以后（发请求，前台将参数带给服务器）
        // 服务器返回成功没有返回其他数据，只是成功code==200
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        // 代表服务器加入数据成功
        if (result.code == 200) {
            return "ok"
        } else {
            return Promise.reject(new Error('faile'));
        }
    }
};
const getters = {
    categoryView(state) {
        return state.goodsInfo.categoryView || {};
    },
    // 防止出现undefined报错。
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || [];
    }
};
export default {
    state,
    mutations,
    actions,
    getters
}
